package com.cnap.model.schedule;

import com.cnap.db.entity.MicroServicePO;
import com.cnap.db.entity.StrategyPO;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微服务dto
 *
 * @author xc
 **/
public class MicroServiceDTO {
    // 微服务信息
    private MicroServicePO microServicePO;
    // 调度策略
    private StrategyPO scheduleStrategyPO;
    // 扩缩容策略
    private List<StrategyPO> scaleStrategyPOs;

    public MicroServiceDTO(MicroServicePO microServicePO, StrategyPO scheduleStrategyPO,
                           List<StrategyPO> scaleStrategyPOs) {
        this.microServicePO = microServicePO;
        this.scheduleStrategyPO = scheduleStrategyPO;
        this.scaleStrategyPOs = scaleStrategyPOs;
    }

    public MicroServiceDTO() {
    }

    /**
     * get
     *
     * @return MicroServicePO
     */
    public MicroServicePO getMicroServicePO() {
        return microServicePO;
    }

    /**
     * set
     *
     * @param microServicePO microServicePO
     */
    public void setMicroServicePO(MicroServicePO microServicePO) {
        this.microServicePO = microServicePO;
    }

    /**
     * get
     *
     * @return ScheduleStrategyPO
     */
    public StrategyPO getScheduleStrategyPO() {
        return scheduleStrategyPO;
    }

    /**
     * set
     *
     * @param scheduleStrategyPO scheduleStrategyPO
     */
    public void setScheduleStrategyPO(StrategyPO scheduleStrategyPO) {
        this.scheduleStrategyPO = scheduleStrategyPO;
    }

    /**
     * get
     *
     * @return ScaleStrategyPOs
     */
    public List<StrategyPO> getScaleStrategyPOs() {
        return scaleStrategyPOs;
    }

    /**
     * set
     *
     * @param scaleStrategyPOs scaleStrategyPOs
     */
    public void setScaleStrategyPOs(List<StrategyPO> scaleStrategyPOs) {
        this.scaleStrategyPOs = scaleStrategyPOs;
    }

    /**
     * 转换成VO
     *
     * @return vo
     */
    public MicroServiceVO toVO() {
        MicroServiceVO microServiceVO = new MicroServiceVO();
        microServiceVO.setId(microServicePO.getId());
        microServiceVO.setApplicationId(microServicePO.getApplicationId());
        microServiceVO.setApplicationName(microServicePO.getApplicationName());
        microServiceVO.setServiceName(microServicePO.getMicroServiceName());
        microServiceVO.setSize(microServicePO.getSize());
        microServiceVO.setStatus(String.valueOf(microServicePO.getStatus()));
        microServiceVO.setProvider(microServicePO.getUserName());
        if (Objects.nonNull(scaleStrategyPOs)) {
            List<StrategyVO> strategyVOS = scaleStrategyPOs.stream().map(po -> new StrategyVO(po.getStrategyName(),
                    po.getStrategyParam(), po.getEnableDefault(), po.getId(), po.getScaleType())).collect(Collectors.toList());
            microServiceVO.setScaleStrategys(strategyVOS);
        }
        if (Objects.nonNull(scheduleStrategyPO)) {
            microServiceVO.setScheduleStrategy(new StrategyVO(scheduleStrategyPO.getStrategyName(),
                    scheduleStrategyPO.getStrategyParam(), scheduleStrategyPO.getEnableDefault(), scheduleStrategyPO.getId()));
            microServiceVO.setClusterInfos(scheduleStrategyPO.toVO());
        }
        microServiceVO.setPaths(Arrays.asList(microServicePO.getPath().split(",")));
        microServiceVO.setUpdateTime(microServicePO.getUpdateTime());
        microServiceVO.setCategory(microServicePO.getCategory());

        return microServiceVO;
    }
}
